package com.jerkin.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @version V1.0
 * @ClassName: ProxyHandler
 * @Description:
 * @author: huhongzhi
 * @date: 2019/12/1 15:36
 */
public class ProxyHandler implements InvocationHandler {
    private Object target;

    private static AtomicInteger atomicInteger = new AtomicInteger(0);


    public ProxyHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Integer i = atomicInteger.incrementAndGet();
        System.out.println("进行方法代理");
        Object t = method.invoke(target, args);
        return t;
    }

    public static Object bind(Object target) {
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(), new ProxyHandler(target));
    }
}
